import type { App } from "vue";
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },
  {
    path: "/report",
    component: () => import("@/views/control/report.vue"),
    meta: { hidden: true },
  },
  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
          alwaysShow: false,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error-page/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error-page/404.vue"),
        meta: { hidden: true },
      },
    ],
  },

  // 以下为临时搭建的静态路由 用于前期开发
  {
    path: "/control",
    name: "/control",
    meta: { title: "驾驶舱", icon: "monitor", alwaysShow: false },
    children: [
      {
        path: "/control",
        component: () => import("@/views/control/index.vue"),
        name: "control",
        meta: {
          title: "驾驶舱",
          alwaysShow: false,
          icon: "document",
          hidden: false,
        },
        children: [],
      },
    ],
  },
  {
    path: "/pipenetwork",
    component: Layout,
    name: "pipenetwork",
    meta: { title: "管网管理", icon: "edit" },
    children: [
      {
        path: "pipeedit",
        component: () => import("@/views/pipe-network/index.vue"),
        name: "pipeedit",
        meta: { title: "管网编辑", icon: "document" },
        children: [],
      },
      // {
      //   path: "pipeimport",
      //   component: () => import("@/views/dashboard/index.vue"),
      //   name: "pipeimport",
      //   meta: { title: "管网数据导入" },
      //   children: [],
      // },
    ],
  },
  {
    path: "/layer1",
    component: Layout,
    name: "layer",
    meta: { title: "图层管理", icon: "cascader" },
    children: [
      {
        path: "gismap",
        component: () => import("@/views/layer/gismap/index.vue"),
        name: "gismap",
        meta: { title: "GIS一张图", icon: "document" },
        children: [],
      },
      {
        path: "area",
        component: () => import("@/views/layer/area/index.vue"),
        name: "area",
        meta: { title: "区域划分", icon: "document" },
        children: [],
      },
    ],
  },
  {
    path: "/device",
    component: Layout,
    name: "device",
    meta: { title: "设备管理", icon: "publish" },
    children: [
      {
        path: "info",
        component: () => import("@/views/device/info/index.vue"),
        name: "info",
        meta: { title: "设备信息", icon: "document" },
        children: [],
      },
      {
        path: "list",
        component: () => import("@/views/device/list/index.vue"),
        name: "list",
        meta: { title: "设备台账", icon: "document" },
        children: [],
      },
    ],
  },
  {
    path: "/report",
    component: Layout,
    name: "report",
    meta: { title: "数据统计", icon: "client" },
    children: [
      {
        path: "dotline",
        component: () => import("@/views/report/dotline/index.vue"),
        name: "dotline",
        meta: { title: "专题展示", icon: "document" },
        children: [],
      },
      {
        path: "device",
        component: () => import("@/views/report/device/index.vue"),
        name: "deviceReport",
        meta: { title: "资产统计", icon: "document" },
        children: [],
      },
    ],
  },
  {
    path: "/monitoy",
    component: Layout,
    name: "monitoy",
    meta: { title: "管网监控", icon: "publish" },
    children: [
      {
        path: "monitoy-map",
        component: () => import("@/views/monitoy/map/index.vue"),
        name: "monitoy-map",
        meta: { title: "地图监控", icon: "monitor" },
        children: [],
      },
      {
        path: "device",
        component: () => import("@/views/monitoy/device/index.vue"),
        name: "monitoy-device",
        meta: { title: "设备监控", icon: "document" },
        children: [],
      },
    ],
  },
  {
    path: "/inspection",
    component: Layout,
    name: "inspectionPlan",
    meta: { title: "巡检计划管理", icon: "security" },
    children: [
      {
        path: "line",
        component: () => import("@/views/inspection-plan/line/index.vue"),
        name: "line",
        meta: { title: "巡检路线设置", icon: "document" },
        children: [],
      },
      {
        path: "task",
        component: () => import("@/views/inspection-plan/task/index.vue"),
        name: "task",
        meta: { title: "巡检任务设置", icon: "document" },
        children: [],
      },
      {
        path: "plan",
        component: () => import("@/views/inspection-plan/plan/index.vue"),
        name: "plan",
        meta: { title: "巡检计划管理", icon: "document" },
        children: [],
      },
      {
        path: "holiday",
        component: () => import("@/views/inspection-plan/holiday/index.vue"),
        name: "holiday",
        meta: { title: "节假日管理", icon: "document" },
        children: [],
      },
    ],
  },
  {
    path: "/inspection-action",
    component: Layout,
    name: "inspectionAction",
    meta: { title: "巡检执行管理", icon: "dict" },
    children: [
      {
        path: "detail",
        component: () => import("@/views/inspection-action/detail/index.vue"),
        name: "detail",
        meta: { title: "巡检明细", icon: "document" },
        children: [],
      },
      {
        path: "danger",
        component: () => import("@/views/inspection-action/danger/index.vue"),
        name: "danger",
        meta: { title: "隐患上报", icon: "document" },
        children: [],
      },
      {
        path: "distribution",
        component: () =>
          import("@/views/inspection-action/distribution/index.vue"),
        name: "distribution",
        meta: { title: "隐患分布" },
        icon: "document",
        children: [],
      },
    ],
  },
  {
    path: "/account",
    component: Layout,
    name: "account",
    meta: { title: "账号管理", icon: "menu" },
    children: [
      {
        path: "person",
        component: () => import("@/views/account/person.vue"),
        name: "person",
        meta: { title: "个人中心", icon: "document" },
        children: [],
      },
      // {
      //   path: "sync",
      //   component: () => import("@/views/account/sync.vue"),
      //   name: "sync",
      //   meta: { title: "账号信息同步", icon: "document" },
      //   children: [],
      // },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" });
}

export default router;
